package com.iplay.dao.order;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import com.iplay.entity.order.ApprovalStatus;
import com.iplay.entity.order.OrderBHReviewedCustomerIdDO;
import com.iplay.entity.order.OrderDO;
import com.iplay.entity.order.OrderIdParticipantStatusDO;
import com.iplay.entity.order.OrderStatus;
import com.iplay.entity.order.SimplifiedOrderDO;

public interface OrderDAO extends CrudRepository<OrderDO, Integer>, JpaSpecificationExecutor<OrderDO> {
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.managerId = ?2, h.manager = ?3, h.lastUpdated = ?4 where h.id = ?1")
	int updateManager(int id, int managerId, String manager, long lastUpdated);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.feastingDate = ?2, h.lastUpdated = ?3 where h.id = ?1")
	int updateFeastingDate(int id, String date, long lastUpdated);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.orderStatus = ?2 where h.id = ?1")
	int updateStatus(int id, OrderStatus status);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.reviewed = ?2, h.orderStatus = ?3, h.lastUpdated = ?4 where h.id = ?1")
	int updateReviewedAndOrderStatus(int id, boolean reviewed, OrderStatus status, long lastUpdated);
	
	/*
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.lastUpdated = ?2 where h.id = ?1")
	int updateOrder(int id, long lastUpdated);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.paymentApprovalStatus = ?2 where h.id = ?1")
	int updateOrderPaymentStatus(int id, RedundantApprovalStatus paymentApprovalStatus);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.contractApprovalStatus = ?2 where h.id = ?1")
	int updateOrderContractStatus(int id, RedundantApprovalStatus contractApprovalStatus);
	*/
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.orderStatus = ?2, h.lastUpdated = ?3, h.paymentApprovalStatus = ?4 where h.id = ?1")
	int updateStatusLastUpdatedPaymentStatus(int id, OrderStatus status, long lastUpdated, ApprovalStatus paymentApprovalStatus);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.lastUpdated = ?2, h.contractApprovalStatus = ?3 where h.id = ?1")
	int updateLastUpdatedContractStatus(int id, long lastUpdated, ApprovalStatus contractApprovalStatus);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.orderStatus = ?2, h.paymentApprovalStatus = ?3 where h.id = ?1")
	int updateStatusPaymentStatus(int id, OrderStatus status, ApprovalStatus paymentApprovalStatus);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.orderStatus = ?2, h.contractApprovalStatus = ?3 where h.id = ?1")
	int updateStatusContractStatus(int id, OrderStatus status, ApprovalStatus contractApprovalStatus);
	
	OrderBHReviewedCustomerIdDO findBHReviewedCustomerIdById(int id);
	
	OrderIdParticipantStatusDO findIdCustomerIdManagerIdRecommenderIdStatusById(int id);
	
	/*
	Page<SimplifiedOrderDO> findByOrderStatusInAndCustomerIdOrRecommenderIdOrManagerId(Collection<OrderStatus> orderStatus,int customerId, int recommenderId, 
			int managerId, Pageable pageable);
	*/
	Page<SimplifiedOrderDO> findByCustomerIdOrRecommenderIdOrManagerId(int customerId, int recommenderId, 
			int managerId, Pageable pageable);
	
	Page<OrderDO> findByCustomerId(int customerId, Pageable pageable);
	
	Page<OrderDO> findByRecommenderId(int recommenderId, Pageable pageable);
	
	Page<OrderDO> findByManagerId(int managerId, Pageable pageable);

	@Transactional
	@Modifying
	@Query("update OrderDO h set h.customer = ?2 where h.customerId = ?1")
	int updateCustomerUsername(int userId, String username);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.recommender = ?2 where h.recommenderId = ?1")
	int updateRecommenderUsername(int userId, String username);
	
	@Transactional
	@Modifying
	@Query("update OrderDO h set h.manager = ?2 where h.managerId = ?1")
	int updateManagerUsername(int userId, String username);
}
